package com.item.audrey_hepburn.service;/*/**

 _______  _______________   _________________________  __________ ____ ___  ________ 
 \      \ \_   _____/\   \ /   /\_   _____/\______   \ \______   \    |   \/  _____/ 
 /   |   \ |    __)_  \   Y   /  |    __)_  |       _/  |    |  _/    |   /   \  ___ 
/    |    \|        \  \     /   |        \ |    |   \  |    |   \    |  /\    \_\  \
\____|__  /_______  /   \___/   /_______  / |____|_  /  |______  /______/  \______  /
        \/        \/                    \/         \/          \/                 \/ 
 
 * @Auther: WD
 * @Date: 19-5-8 19:01
 * @Description: 
 */

import com.alibaba.fastjson.JSONArray;
import com.item.audrey_hepburn.entity.db2.Ticket;
import com.item.audrey_hepburn.entity.db2.User;
import com.item.audrey_hepburn.factory.HighSpeed;
import com.item.audrey_hepburn.factory.MotorCar;
import com.item.audrey_hepburn.factory.TicketFactory;
import com.item.audrey_hepburn.factory.TrainTickets;
import com.item.audrey_hepburn.util.MailService;
import com.item.audrey_hepburn.util.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;

import java.util.List;
import java.util.concurrent.Future;

@Service
// 线程执行任务类
public class AsyncTaskService {

   Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MailService mailService;
    @Autowired
    private TemplateEngine templateEngine;



    /**
     * 异常调用返回Future
     *
     * @param i
     * @return
     * @throws InterruptedException
     */
    @Async("taskExecutor")
    public Future<String> asyncInvokeReturnFuture(int i) throws Exception {
        System.out.println("input is " + i);
        Thread.sleep(5000);
         logger.info("来到此线程："+i);
        Future<String> future = new AsyncResult<String>("success:" + (i++));// Future接收返回值，这里是String类型，可以指明其他类型

        return future;
    }

    @Async("taskExecutor")
    public Future<String> asyncTicketGrabbing(String email,String[] selectT,JSONArray jsonArray, String trainnumber, String tickettype) throws Exception {
        TicketFactory ticketFactory=null;
        switch (tickettype){
            case "火车票":ticketFactory=new TrainTickets();break;
            case "高铁票":ticketFactory=new HighSpeed();break;
            case "动车票":ticketFactory=new MotorCar();break;
        }


        logger.info("--来到此线程--");

        while (true){
            Thread.sleep(5000);
            List<Ticket>tickets = ticketFactory.findByTypeInfo(jsonArray);
            logger.info("检索数据："+tickets);
            String result = getTicketInfo(tickets,selectT,trainnumber);
            logger.info("进一步加强数据："+result);
            if(null!=result){
                mailService.sendSimpleMail(email, "发送邮件测试", "恭喜您，已为您查到余票:"+result);
                logger.info("恭喜您，已为您查到余票:"+result);
                break;
            }
        }

        Future<String> future = new AsyncResult<String>("success");// Future接收返回值，这里是String类型，可以指明其他类型

        return future;
    }

    /**

     *@描述 查询是否有余票

     *@参数  [tickets, selectT, trainnumber]

     *@返回值  java.lang.String

     *@创建人  wangdong

     *@创建时间  19-5-8/下午10:11

     *@修改人和其它信息

     */
    public String getTicketInfo(List<Ticket> tickets,String[] selectT,String trainnumber){
        for(int i=0;i<tickets.size();i++){
            if (tickets.get(i).getTRAINNUMBER().equals(trainnumber)){
                for(int k=0;k<selectT.length;k++){
                    logger.info("检索的车座类型："+selectT[k]);
                    if(selectT[k].equals("1")){
                        if(!tickets.get(i).getORDINARYSOFTSLEEPER().equals("无")){
                          return "软卧";
                        }
                    }else if(selectT[k].equals("2")){
                        if(!tickets.get(i).getHARDSLEEPER().equals("无")){
                            return "硬卧";
                        }
                    }else if(selectT[k].equals("3")){
                        if(!tickets.get(i).getHARDSEAT().equals("无")){
                            return "坐票";
                        }
                    }else if(selectT[k].equals("4")){
                        if(!tickets.get(i).getNOSEAT().equals("无")){
                            return "站票";
                        }
                    }else if(selectT[k].equals("5")){
                        if(!tickets.get(i).getBUSINESSBLOCK().equals("无")){
                            return "商务座";
                        }
                    }else if(selectT[k].equals("6")){
                        if(!tickets.get(i).getFIRSTCLASSSEAT().equals("无")){
                            return "一等座";
                        }
                    }else if(selectT[k].equals("7")){
                        if(!tickets.get(i).getSECONDCLASSSEAT().equals("无")){
                            return "二等座";
                        }
                    }
                }
            }
        }
        return null;
    }


}
/*

class TestThred extends Thread {

    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LoggerFactory.getLogger(main.class).info("来了老弟");
    }
}

class main{
    public static void main(String[] args) {
            TestThred testThred = new TestThred();
            testThred.start();
        System.out.println("来了小伙");

    }
}*/
